package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.gusha.dao.SysAdminDao;
import com.gusha.model.SysAdmin;
import com.gusha.model.SysRole;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:SysAdminService服务类
 */

@Service
public class SysAdminService {

    @Autowired
    private SysAdminDao sysAdminDao;

    public Page<SysAdmin> findAll(Pageable pageable, SysAdmin sysAdmin) {
        @SuppressWarnings("serial")
        Page<SysAdmin> result = sysAdminDao.findAll(new Specification<SysAdmin>() {
            @Override
            public Predicate toPredicate(Root<SysAdmin> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysAdmin.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysAdmin.getId()));
                }
                if (StringUtils.isNotBlank(sysAdmin.getUserName())) {
                    list.add(cb.like(root.get("userName").as(String.class),"%"+sysAdmin.getUserName()+"%"));
                }
                if (StringUtils.isNotBlank(sysAdmin.getPassword())) {
                    list.add(cb.equal(root.get("password").as(String.class),sysAdmin.getPassword()));
                }
                if (sysAdmin.getRole() != null) {
                    list.add(cb.equal(root.get("role").as(SysRole.class),sysAdmin.getRole()));
                }
                if (sysAdmin.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),sysAdmin.getCreateTime()));
                }
                if (sysAdmin.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),sysAdmin.getUpdateTime()));
                }
                if (sysAdmin.getLastLoginTime() != null) {
                    list.add(cb.equal(root.get("lastLoginTime").as(Date.class),sysAdmin.getLastLoginTime()));
                }
                if (StringUtils.isNotBlank(sysAdmin.getLastLoginIp())) {
                    list.add(cb.equal(root.get("lastLoginIp").as(String.class),sysAdmin.getLastLoginIp()));
                }
                if (sysAdmin.getIsDelete() != null) {
                    list.add(cb.equal(root.get("isDelete").as(Integer.class),sysAdmin.getIsDelete()));
                }
                if (StringUtils.isNotBlank(sysAdmin.getCreator())) {
                    list.add(cb.equal(root.get("creator").as(String.class),sysAdmin.getCreator()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
